package shuaige.jdk.stream.collectors相关的方法;

import shuaige.jdk.model.Employee;

import java.util.*;
import java.util.stream.Collectors;

public class PartitioningBy分割流的数据 {

    static List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

    public static void main(String[] args) {
        默认转为list2();
    }

    public static void 默认转为list1() {
        Map<Boolean, List<Integer>> map = list.stream().collect(Collectors.partitioningBy(e -> e > 5));
        System.out.println(map);
    }

    public static void 默认转为list2() {
        List<Employee> emps = getEmps();
        Map<Boolean, List<Employee>> collect = emps
                .stream()
                .collect(Collectors.partitioningBy(employee -> {
                    return employee.getSales() > 30;
                }));
        System.out.println(collect);
    }

    public static void 指定转换类型() {
        Map<Boolean, Set<Integer>> map2 = list.stream().collect(Collectors.partitioningBy(e -> e > 6, Collectors.toSet()));
        System.out.println(map2);
    }

    /**
     * 生成测试数据
     */
    private static List<Employee> getEmps() {
        List<Employee> list = new ArrayList<>();
        Random rd = new Random();
        String[] citys = {"北京", "上海", "广州", "杭州", "深圳"};
        String[] firstName = {"张", "李", "杨", "宁", "刘", "王", "高", "葛"};
        Integer[] sales = {100, 50, 30, 20};

        for (int i = 0; i < 10; i++) {
            String city = citys[rd.nextInt(5)];
            Integer sale = sales[rd.nextInt(4)];
            String fname = firstName[rd.nextInt(firstName.length)];

            list.add(new Employee(fname, city, sale));
        }

        return list;
    }
}
